/**
 * Project: Utilities <br>
 * Package: org.leumasjaffe.util
 */
package org.leumasjaffe.util;

import java.util.Comparator;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
 * Class org.leumasjaffe.util..DefaultComparator in file DefaultComparator.java <br>
 * 
 * @author leumasjaffe
 * @version Jan 25, 2014
 * Created on: Jan 25, 2014
 * 
 */
@NonNullByDefault
public class DefaultComparator<E extends Comparable<E>> implements Comparator<E> {

	@SuppressWarnings( "rawtypes" )
	private static final DefaultComparator<?> INSTANCE = new DefaultComparator();

	/**
	 * Get an instance of DefaultComparator for any type of Comparable.
	 *
	 * @param <T> the type of Comparable of interest.
	 *
	 * @return an instance of DefaultComparator for comparing instances of the requested type.
	 */
	public static <T extends Comparable<T>> Comparator<T> getInstance() {
		@SuppressWarnings("unchecked")
		Comparator<T> result = (Comparator<T>) INSTANCE;
		return result;
	}

	private DefaultComparator() {
	}

	@Override
	public int compare( @Nullable E o1, @Nullable E o2 ) {
		if( o1 == o2 ) {
			return 0;
		} else if( o1 == null ) {
			return 1;
		} else if( o2 == null ) {
			return -1;
		} else {
			return o1.compareTo( o2 );
		}
	}

}
